package com.neusoft.elmboot.service.impl;

import com.neusoft.elmboot.mapper.OrdersMapper;
import com.neusoft.elmboot.mapper.UserWalletMapper;
import com.neusoft.elmboot.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayServiceImpl implements PayService {
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    UserWalletMapper userWalletMapper;

    @Override
    public String payOrdersById(String userId, int orderId) {
        Double money = userWalletMapper.getCreditByUserId(userId);
        Double cost = ordersMapper.getOrderTotalById(orderId);
        if(ordersMapper.getOrdersStateById(orderId) != "Unpaid") {
            return ordersMapper.getOrdersStateById(orderId);
        }
        else if(money < cost) {
            return "Inadequate Money";
        }
        else {
            money -= cost;
            userWalletMapper.updateCredit(userId, money);
            ordersMapper.setOrderState(orderId, "Paid");
            return "Pay Successfully";
        }
    }
}
